package com.miyake.loveviewweb.client.chart;

import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.shared.CommonTool;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.rednels.ofcgwt.client.ChartWidget;
import com.rednels.ofcgwt.client.model.ChartData;
import com.rednels.ofcgwt.client.model.elements.LineChart;
import com.rednels.ofcgwt.client.model.elements.dot.BaseDot;
import com.rednels.ofcgwt.client.model.elements.dot.Dot;

public class FlashChart extends ChartWidget implements IChart {
    private ChartData chartDataRef = new ChartData();
    private ChartModel chartModel;
    
    public FlashChart() {
        super.setSize("100%", "100%");
    }

    public void setData(String xname, double[] x, String yname, double[] y) {
        LineChart lineChart  = new LineChart();
        for (int i = 0; i < y.length; i++) {
            BaseDot dot = new Dot();
            dot.setXY(x[i], y[i]);
            lineChart.addDots(dot);
        } 
        
        chartDataRef.addElements(lineChart);
        chartDataRef.getYAxis().setMax(CommonTool.getMax(y));
        chartDataRef.getYAxis().setMin(CommonTool.getMin(y));
        super.setChartData(chartDataRef);
    }

    public void setData(String yname, double[] y) {
        double[] x = new double[y.length];
        for (int i = 0; i < x.length; i++) {
            x[i] = i;
        }
        setData("x", x, yname, y);
        
    }

    public void clearPlot(int i) {
        chartDataRef.getElements().clear();
    }

    public Widget getUiWidget() {
        return super.asWidget();
    }

    public void setChartSize(Integer width, Integer height) {
        super.setSize(width.toString() + "px", height.toString() + "px");
        new ChartSizeUpdater(chartModel.getKey(), width, height);
    }

    public void setData(final ChartModel chartModel) {
        this.chartModel = chartModel;
        showChart(chartModel);
/*        DataServiceWrapper.getInstance().getService().getChart(chartModel.getKey(), true, new AsyncCallback<ChartModel>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(ChartModel result) {
                showChart(result);
            }
            
        });*/

    }



    public void update() {
        // TODO Auto-generated method stub
        
    }



    private void showChart(ChartModel chartModel) {
        if (chartModel.getChartDataType().equals(ChartModel.ChartDataType.XY_DATA)) {
            setData(
                chartModel.getxLabel(), chartModel.getXarray(), 
                chartModel.getyLabel(), chartModel.getYarray());
        }
        else {
            setData("y", chartModel.getYarray());
        }
        setTitle(chartModel.getName());
 //       setChartSize(chartModel.getChartWidth(), chartModel.getChartHeight());
        update();
    }
}
